<template>
  <div class="pagination">
    <!-- 如果当前页不是1 就显示上一页 -->
    <button :disabled=' pageNo == 1'
      @click="$emit('pageNoInfo',pageNo-1)" > 
      «上一页
    </button>
    <!-- 如果连续页数 大于总页数 的时候 开始页数就是 1 结束页数就是总页数 -->
    <button v-if="startNumAndEndNum.start > 1" 
      @click="$emit('pageNoInfo',1)" 
      :class="{ active: pageNo == 1}" >
      1
    </button>
    <button v-if="startNumAndEndNum.start > 2">···</button>
    <button 
    v-for="(item,index) in startNumAndEndNum.end" 
    :key="index" 
    v-if="startNumAndEndNum.start <= item" 
    @click="$emit('pageNoInfo',item)"
    :class="{ active: pageNo == item }" >
      {{ item }} 
    </button>
    
    <button v-if="startNumAndEndNum.end < totalPage-1">···</button>
    <button v-if="startNumAndEndNum.end < totalPage"  
    @click="$emit('pageNoInfo',totalPage)"
    :class="{active:pageNo==totalPage}" >
      {{totalPage}}
    </button>
    <button  @click="$emit('pageNoInfo',pageNo+1)" 
    :disabled=' pageNo == totalPage'>
      下一页»
    </button>
    <button>
      共{{total}}条
    </button>
    st:{{startNumAndEndNum.start}}--end :{{startNumAndEndNum.end}}--totalPage:{{totalPage}}
</div>
</template>

<script>
export default {
  // pageNo 当前页 pageSize当前页数据 total总数据 continues 连续页数
  props:['pageNo','pageSize','total','continues'],
  data(){
    return{

    }
  },
  computed:{
    // 计算一共有几页
    totalPage(){
      return Math.ceil(this.total/this.pageSize)
    },
    //计算出连续的页码的起始数字与结束数字【连续页面的数字：至少是5或者7】
    startNumAndEndNum(){
      // 解构 当前页码 pageNo
      const {pageNo,totalPage,continues} = this;
      let start = 0
      let end = 0
      if(continues > totalPage){
        start = 1
        end = totalPage
      }else{
         // 如果当前页码是 1 continues是5
        start = pageNo - parseInt(continues/2)
        end = pageNo + parseInt(continues/2)
        // 目前为止：会出现问题  continues < totalPage
        if(start < 1){
          start = 1
          end = continues
        }
        if(end > totalPage){
          end = totalPage;
          start = totalPage - continues + 1
        }
      }
      return {start , end}
    }
  },
  created(){
    
  },
  methods:{

  }
}
</script>

<style lang="scss" scoped>
.pagination {
  text-align: center;
  button {
    margin: 0 5px;
    background-color: #f4f4f5;
    color: #606266;
    outline: none;
    border-radius: 2px;
    padding: 0 4px;
    vertical-align: top;
    display: inline-block;
    font-size: 13px;
    min-width: 35.5px;
    height: 28px;
    line-height: 28px;
    cursor: pointer;
    box-sizing: border-box;
    text-align: center;
    border: 0;

    &[disabled] {
      color: #c0c4cc;
      cursor: not-allowed;
    }

    &.active {
      cursor: not-allowed;
      background-color: #409eff;
      color: #fff;
    }
  }
}
.active{
  background: skyblue;
}

</style>
